package com.threeox.commonlibrary.ui.view.pullrefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

import com.threeox.commonlibrary.ui.view.inter.pullrefresh.IPullable;

/**
 * 
 * @ClassName: PullToRefreshExpandListView
 * 
 * @Description: TODO(可以下拉刷新和上拉加载的ExpandableListView)
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间:2016-9-13 上午11:25:10
 * 
 */
@SuppressWarnings("ResourceAsColor")
public class PullToRefreshExpandListView extends ExpandableListView implements IPullable {

	public PullToRefreshExpandListView(Context context) {
		this(context, null);
	}

	public PullToRefreshExpandListView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public PullToRefreshExpandListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setCacheColorHint(android.R.color.transparent);
		this.setSelector(android.R.color.transparent);
	}

	@Override
	public boolean isPullDown() {
		try {
			if (getCount() == 0) {
				// 没有item的时候也可以下拉刷新
				return true;
			} else if (getFirstVisiblePosition() == 0 && getChildAt(0).getTop() >= 0) {
				// 滑到顶部了
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean isPullUp() {
		try {
			if (getCount() == 0) {
				// 没有item的时候也可以上拉加载
				return true;
			} else if (getLastVisiblePosition() == (getCount() - 1)) {
				// 滑到底部了
				return getChildAt(getLastVisiblePosition() - getFirstVisiblePosition()) != null &&
						getChildAt(getLastVisiblePosition() - getFirstVisiblePosition()).getBottom() <= getMeasuredHeight();
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}

}
